home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MISC / SHELL.ARC / Shell / Sources / c / FontLabel < prev    next >
Encoding:
Text File  |  1994-06-25  |  3.2 KB  |  176 lines

  1. #include <string.h>
  2. #include <stdarg.h>
  3. #include <stdio.h>
  4. #include <limits.h>
  5.  
  6. #include "DeskLib:WimpSWIs.h"
  7. #include "DeskLib:Font.h"
  8. #include "DeskLib:GFX.h"
  9.  
  10. #include "Shell.FontLabel.h"
  11. #include "Shell.Extra.h"
  12. #include "Shell.SafeAlloc.h"
  13.  
  14.  
  15.  
  16.  
  17. typedef struct    {
  18.     char    *text;
  19.     int    yoffset;
  20.     int    forecol, backcol;
  21.     }
  22.     fontlabel_ref;
  23.  
  24.  
  25.  
  26. #define fontplot_JUSTIFY    0x00000001
  27. #define fontplot_RUBOUT        0x00000002
  28. #define fontplot_ABSCOORS    0x00000004
  29. #define fontplot_OSCOORS    0x00000010
  30.  
  31.  
  32.  
  33. static char        Shell_fontname[ 256] = "Trinity.Medium";
  34. static font_handle    handle = NULL;
  35. static int        Shell_Fxsize = 16*14, Shell_Fysize = 16*14;
  36. static font_info    Shell_fontinfo;
  37.  
  38.  
  39.  
  40. #define Shell_EnsureHandle()                                \
  41. if ( !handle)    {                                    \
  42.     Font_FindFont( &handle, Shell_fontname, Shell_Fxsize, Shell_Fysize, 0, 0);    \
  43.     Font_ReadInfo( handle, &Shell_fontinfo);                    \
  44.     }                                        \
  45.  
  46.  
  47.  
  48.  
  49. static void Shell_KillHandle()
  50. {
  51. Font_LoseFont( handle);
  52. handle = NULL;
  53. }
  54.  
  55.  
  56.  
  57.  
  58. static int Shell_GetXSizeOfFontString( const char *s)
  59. {    font_string    fontstring;
  60.     int        xbox, dummy;
  61.  
  62. fontstring.s        = (char *) s;
  63. fontstring.x        = INT_MAX;    /* available x-space    */
  64. fontstring.y        = INT_MAX;    /* available y-space    */
  65. fontstring.split    = -1;        /* don't look for places to split text    */
  66. fontstring.term        = strlen( s);    /* number of chrs to look at        */
  67. Font_StringWidth( &fontstring);        /* .x is width of string in font coors    */
  68. Font_ConvertToOS( fontstring.x, 0, &xbox, &dummy);
  69. return xbox;
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. static void Shell_FontLabelDisplayer(
  79.     Shell_convertpoint    convert,
  80.     wimp_point        rectsize,
  81.     void            *reference,
  82.     const wimp_rect        *redrawrect
  83.     )
  84.  
  85. {    fontlabel_ref    *info    = (fontlabel_ref *) reference;
  86.     int        screenx, screeny;
  87. UNUSED( redrawrect);
  88.  
  89. Shell_EnsureHandle();
  90. Font_SetFont( handle);
  91.  
  92. if ( info->forecol < 8 && info->backcol < 8 )
  93.     Font_SetFontColours( handle, info->backcol, info->backcol, info->forecol - info->backcol);
  94. else    Font_SetFontColours( handle, info->backcol, info->forecol, 0);
  95.  
  96. Shell_Move( 0, 0, convert);
  97. Shell_Move( rectsize.x, rectsize.y, convert);
  98.  
  99. screenx = Shell_ConvertXToScreen( 0, convert);
  100. screeny = Shell_ConvertYToScreen( info->yoffset, convert);
  101.  
  102. Font_Paint( info->text, fontplot_RUBOUT | fontplot_OSCOORS, screenx, screeny);
  103.  
  104. Shell_KillHandle();
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. Shell_rectblock *Shell_FontLabel(
  120.     Shell_windblock *w,
  121.     int x, int y,
  122.     int forecol, int backcol,
  123.     const char *text
  124.     )
  125.  
  126. {    wimp_rect    rect;
  127.     int        len        = strlen( text);
  128.     fontlabel_ref    *textinfo    = Shell_SafeMalloc( sizeof( fontlabel_ref));
  129.  
  130. Shell_EnsureHandle();
  131.  
  132. rect.min.x    = x;
  133. rect.max.x    = x + Shell_GetXSizeOfFontString( text);
  134. rect.max.y    = y+Shell_fontinfo.maxy;
  135. rect.min.y    = y+Shell_fontinfo.miny;
  136.  
  137. textinfo->text = Shell_SafeMalloc( 1+len);
  138. strcpy( textinfo->text, text);
  139.  
  140. textinfo->yoffset = -Shell_fontinfo.miny;
  141.  
  142. textinfo->forecol = forecol;
  143. textinfo->backcol = backcol;
  144.  
  145.     {
  146.     Shell_rectblock    *r;
  147.     r = Shell_AddRectangle( w, &rect, Shell_FontLabelDisplayer, (void *) textinfo);
  148.     Shell_MakeRectIcon( r, forecol, backcol, "R1");
  149.     return r;
  150.     }
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157. Shell_rectblock    *Shell_FontLabelf(
  158.     Shell_windblock *w,
  159.     int x, int y,
  160.     int forecol, int backcol,
  161.     const char *fmt, ...
  162.     )
  163. {
  164.     va_list args;
  165.  
  166. va_start( args, fmt);
  167. vsprintf( Shell_string, fmt, args);
  168. va_end( args);
  169.  
  170. return Shell_FontLabel( w, x, y, forecol, backcol, Shell_string);
  171.  
  172. }
  173.  
  174.  
  175.  
  176.